package com.log4jviewer.domain;

import java.sql.Timestamp;

import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

/**
 * The wrapper class for log4j LoggingEvent object which represents log. Provides information about log fields.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public class SocketLogEvent implements LogEvent {

    private LoggingEvent log;

    public SocketLogEvent(final LoggingEvent log) {
        this.log = log;
    }

    @Override
    public String getCategoryName() {
        return StringUtility.emptyStringIfNull(log.getLoggerName());
    }

    @Override
    public String getClassName() {
        String classInfo = "";

        if (log.getLocationInformation() != null) {
            classInfo = StringUtility.emptyStringIfNull(log.getLocationInformation().getClassName());
        }
        return classInfo;
    }

    @Override
    public String getDate() {
        long time = log.getTimeStamp();
        String date = "";

        if (time != 0) {
            date = new Timestamp(time).toString();
        }
        return date;
    }

    @Override
    public String getFileName() {
        String fileInfo = "";

        if (log.getLocationInformation() != null) {
            fileInfo = StringUtility.emptyStringIfNull(log.getLocationInformation().getFileName());
        }
        return fileInfo;
    }

    @Override
    public String getLineNumber() {
        String lineInfo = "";

        if (log.getLocationInformation() != null) {
            lineInfo = StringUtility.emptyStringIfNull(log.getLocationInformation().getLineNumber());
        }
        return lineInfo;
    }

    @Override
    public String getMdc() {
        return "";
    }

    @Override
    public String getMessage() {
        return StringUtility.emptyStringIfNull(log.getRenderedMessage());
    }

    @Override
    public String getMethodName() {
        String methodInfo = "";

        if (log.getLocationInformation() != null) {
            methodInfo = StringUtility.emptyStringIfNull(log.getLocationInformation().getMethodName());
        }
        return methodInfo;
    }

    @Override
    public String getNdc() {
        return StringUtility.emptyStringIfNull(log.getNDC());
    }

    @Override
    public String getLevel() {
        Level level = log.getLevel();
        String levelInfo = "";

        if (level != null) {
            levelInfo = level.toString();
        }

        return levelInfo;
    }

    @Override
    public String getThreadName() {
        return StringUtility.emptyStringIfNull(log.getThreadName());
    }

    @Override
    public String getThrowableInfo() {
        ThrowableInformation throwable = log.getThrowableInformation();
        String throwableInfo = "";

        if (throwable != null) {
            throwableInfo = throwable.getThrowable().getMessage();
        }
        return throwableInfo;
    }
}
